# 作业3： 实现一个自动取款机的存取款模拟效果。要求有登陆和退出、查询余额、取钱，存钱等操作

# 预存几个账户信息
bankAcountList = [
    {'id': '20200001', 'name': 'xiaohong', 'passwd': '123hong', 'balance': 200},
    {'id': '20200002', 'name': 'xiaoming', 'passwd': '123ming', 'balance': 600},
    {'id': '20200003', 'name': 'xiaotian', 'passwd': '123tian', 'balance': 800}
]

# 登录
def checkLogin(id, passwd):
    for info in bankAcountList:
        if id == info['id']:
            if passwd == info['passwd']:
                return True
        return False

    return False

# 查询
def getAccountInfoByID(id):
    for info in bankAcountList:
        if id == info['id']:
            return info

    return None

def showAccountInfo(id):
    info = getAccountInfoByID(id)
    if info:
        print("账户：", info['id'])
        print("余额：", info['balance'])
    else:
        print("不存在该账户！")

# 存钱
def addAccountBalance(id):
    strNum = input("请输入存款数额：")
    if strNum.isdigit() and int(strNum) > 0:
        addNum = int(strNum)
        info = getAccountInfoByID(id)
        if info:
            info['balance'] += addNum
            print("存款成功！")
            showAccountInfo(id)
        else:
            print("不存在该账户！")
    else:
        print("输入数额错误！")


# 取钱
def delAccountBalance(id):
    strNum = input("请输入取款数额：")
    if strNum.isdigit() and int(strNum) > 0:
        delNum = int(strNum)
        info = getAccountInfoByID(id)
        if info:
            if info['balance'] >= delNum:
                info['balance'] -= delNum
                print("存款成功！")
                showAccountInfo(id)
            else:
                print("账户余额不足！")
        else:
            print("不存在该账户！")
    else:
        print("输入数额错误！")

# 密码输入
# 导入输入输出重定向模块，获取输入字符并以*号代替输出到控制台
import msvcrt
def pwd_input():
    print("请输入账户密码：", end='')
    strPasswd = []
    while True:
        str = msvcrt.getch().decode(encoding='utf-8')
        if str in '\r\n':
            # 换行结束输入
            break
        elif str == '\b':
            # 回退一个字符
            if len(strPasswd) > 0:
                del strPasswd[-1]
                msvcrt.putch('\b'.encode(encoding='utf-8'))  # 光标回退一格
                msvcrt.putch(' '.encode(encoding='utf-8'))   # 输出一个空格覆盖原来的星号
                msvcrt.putch('\b'.encode(encoding='utf-8'))  # 光标回退一格准备接受新的输入
        else:
            strPasswd.append(str)
            # 显示为星号
            msvcrt.putch('*'.encode(encoding='utf-8'))

    return (''.join(strPasswd))

#菜单显示
def showOptionMenu():
    print('{:*^30s}'.format('ATM存取款机'))
    print('**      {:<20s}**'.format('1、查询'))
    print('**      {:<20s}**'.format('2、取钱'))
    print('**      {:<20s}**'.format('3、存钱'))
    print('**      {:<20s}**'.format('4、退出'))
    print('*' * 32)

def useATMSys():
    strID = input("请输入账户ID: ")
    strPasswd = pwd_input()
    if checkLogin(strID, strPasswd):
        while True:
            showOptionMenu()
            strOpe = input("请输入操作序号：")
            if strOpe == "1":
                showAccountInfo(strID)
            elif strOpe == "2":
                delAccountBalance(strID)
            elif strOpe == '3':
                addAccountBalance(strID)
            elif strOpe == '4':
                break
            else:
                print("输入操作序号错误！")

            strContinue = input("按回车键回到主菜单，按q退出系统：")
            if strContinue in '\r\n':
                pass
            elif str == 'q':
                break
    else:
        print("账户或密码错误！")


useATMSys()